<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
      // 通过ID获取到地址
      function getItemDataById(arr, id, name = 'name') {
        // let result = '***'
        const result = {
          value: '****',
          idxs: []
        }
        let level = -1
        const recursive = (arr, id) => {
          level++
          arr.some((item, i) => {
            if (result.id === id) {
                return true
            }
            if (item.id == id) {
              result.value = item[name]
              result.id = id
              result.idxs[level] = i
              result.idxs = result.idxs.slice(0, level)
              return true
            } 
            if (item.children && item.children.length > 0) {
              result.idxs[level] = i
              recursive(item.children, id)
            }
          })
          level--


          // level++
          // for(let i=0; i<arr.length; i++) {
          //   if (result.id === id) {
          //     return true
          //   }
          //   const item = arr[i]
          //   if (item.id == id) {
          //     result.value = item[name]
          //     result.id = id
          //     result.idxs[level] = i
          //     return true
          //   } 
          //   if (item.children && item.children.length > 0) {
          //     result.idxs[level] = i
          //     recursive(item.children, id)
          //   }
          // }
          // level--
        }
        recursive(arr, id)
        return result
      }

const arr = [
  {
    id: 1,
    name: 'a',
    children: [
      {
        id: 11,
        name: 'a1',
        children: [
          {
            id: 111,
            name: 'a1'
          },
          {
            id: 121,
            name: 'a2'
          }
        ]
      },
      {
        id: 12,
        name: 'a2',
        children: [
          {
            id: 121,
            name: 'a1'
          },
          {
            id: 122,
            name: 'a2'
          }
        ]
      }
    ]
  },
  {
    id: 2,
    name: 'b',
    children: [
      {
        id: 21,
        name: 'b1'
      },
      {
        id: 22,
        name: 'b2',
        children: [
          {
            id: 31,
            name: 'b31'
          },
          {
            id: 32,
            name: 'b32'
          }
        ]
      }
    ]
  }
]

const hh = getItemDataById(arr, 11)
console.log('hh', hh)



    </script>
</body>
</html>